home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 2000
/
MacHack 2000.toast
/
pc
/
The Hacks
/
IPFlakeWay
/
Squelch Module
/
source
/
NatTable.h
< prev
next >
Wrap
Text File
|
2000-06-23
|
7KB
|
169 lines
// =================================================================================
// NatTable.h ©1997 Sustainable Softworks. All rights reserved.
// =================================================================================
// Network Address Translation table
// This module encapsulates the IP network address translation table
// as a set of cover routines to hide the internal structure and algorithm
// used to access the table.
//
// Since this module executes in the STREAMs context, instance data is
// allocated when the STREAM is opened rather than by instantiating
// a C++ object. For this reason, each of the module functions is
// passed a pointer to its instance data.
#ifndef _H_NatTable
#define _H_NatTable
#pragma once
#include <types.h>
#include "MyTypes.h"
// include ProxyModule.h before this file
struct translationEntry { // size is 40 bytes
xEndpoint_t actual; // the actual endpoint on the private LAN
UInt16 portRange; // number of ports in port range
xEndpoint_t apparent; // the external visible endpoint
UInt16 identification; // from IP header
UInt16 fragmentOffset;
UInt32 seqInitial; // used to offset seq and ack #'s
SInt16 seqOffset; // for content masquerading
UInt32 seqInitial2; // used to offset seq and ack #'s
SInt16 seqOffset2; // for content masquerading
SInt16 seqOffsetPrev;
UInt16 age; // timer intervals since table entry last refreshed
UInt16 flags; // track entry type and state
UInt16 entryID; // index used to identify a specific table entry
UInt8 protocol; // from IP header (TCP, UDP, ICMP,...)
UInt8 pad; // pad to 40 bytes
};
typedef struct translationEntry translationEntry_t;
// Values for NAT entry Flags
#define kFlagPermanent 1 // entry is permanent and cannot be aged out
#define kFlagFINLocal 2 // Seen TCP FIN from local host
#define kFlagFINPeer 4 // Seen TCP FIN from peer
#define kFlagActive 8 // Entry contains valid data
#define kFlagStatic 0x10 // Apparent EP is static
#define kFlagNoRestore 0x20 // Don't restore this entry
#define kFlagDNSForwarding 0x40 // DNS Forwarding Entry
#define kFlagNonSyn 0x80 // Sent more than a Syn
#define kFlagWamnet 0x0100 // WAM!NET private
// Values for protocol
#define kProtocolNone 0
#define kProtocolAny 0
#define kProtocolICMP 1
#define kProtocolTCP 6
#define kProtocolUDP 17
#define kProtocolGRE 47
#define kNatTableDim 512
#define kPortTableDim 16
#define kPortTableSize 512
// Entry time out in RIP intervals
#define kTimeOutTCP 60 // Default TCP (30 mins)
#define kTimeOut4M 8 // 4 minutes
#define kTimeOut2M 5 // 2 minutes
#define kTimeOut1M 2 // 30-60 seconds
// The first entry of the Table is used for table bookkeeping
struct tableData {
UInt32 portNameHash; // hashed name of port to masquerade on
UInt32 exposedHost; // exposed host address
NetNumber_t natNetwork; // the apparent IP address to use
NetNumber_t actualNetwork; // actual network that should not be translated
UInt16 lastEntry; // index of last physical entry in table
UInt16 lastUsed; // index of last logical entry in table
UInt16 firstActive; // start of active list
UInt16 firstDeleted; // start of deleted list
UInt16 cacheActual; // remember result of last FindActual
UInt16 cacheApparent; // remember result of last FindApparent
UInt16 mssClamp; // TCP mss limit
Boolean isNatOn; // enable/disable NAT
};
typedef struct tableData tableData_t;
// Design Notes:
// We include additional information beyond the endpoint (address, port)
// translation pair to reduce the need for port multiplexing.
// As long as the actual endpoint can be uniquely identified, we can
// re-use the same port numbers to reduce our compatibility risk.
//
// A table entry with port number 0 means match any port (no port translation).
// A table entry with protocol 0 means match any protocol.
// A table entry with peer 0 means match any peer.
// WriteEntry with entryID zero means create a new entry.
//
// For efficiency, the NAT table is threaded by a series of links
// using the entryID. The entryID of each table entry points to
// the next entry in the thread or zero for end of list.
// Two threads are kept, one for active entries, and another for
// deleted entries waiting to be re-used.
//
// When a found entry is returned, the entryID parameter is the index
// of that entry. This index allows specific entries to be modified
// without searching.
//
// If a port is already in use, we try to allocate the next
// sequentially available unused port using a bitmap to
// to track the ports in use.
// ---------------------------------------------------------------------------
// forward function declarations [external]
// ---------------------------------------------------------------------------
void InitTable(translationEntry_t table[]);
// Initialize the NAT table to be empty.
void SetPortName(translationEntry_t table[], UInt32 inPortNameHash);
// Set hashed port name to determine which interface this table is for.
UInt32 GetPortName(translationEntry_t table[]);
// Get hashed port name to determine which interface this table is for.
void SetNatNetwork(translationEntry_t table[], NetNumber_t* inNatNetwork);
NetNumber_t* GetNatNetwork(translationEntry_t table[]);
void SetActualNetwork(translationEntry_t table[], NetNumber_t* inActualNetwork);
NetNumber_t* GetActualNetwork(translationEntry_t table[]);
void SetExposedHost(translationEntry_t table[], UInt32 inExposedHost);
UInt32 GetExposedHost(translationEntry_t table[]);
void SetMSSClamp(translationEntry_t table[], UInt16 inMSSClamp);
UInt16 GetMSSClamp(translationEntry_t table[]);
void SetNatOn(translationEntry_t table[], Boolean inNatOn);
Boolean GetNatOn(translationEntry_t table[]);
Boolean FindApparentEndpoint(translationEntry_t table[], translationEntry_t* mapData);
// Look for a matching NAT table entry, if none found
// create one. Use a different port number only if
// necessary. Resets the entry Age to zero.
Boolean FindApparentDNSEndpoint(translationEntry_t table[], translationEntry_t* mapData, UInt32 destAddr);
Boolean FindActualEndpoint(translationEntry_t table[], translationEntry_t* mapData);
// Look for matching entry. If no complete match, look
// for unambigeous partial match. Return false if none found.
UInt16 FindEntry(translationEntry_t table[], translationEntry_t* mapData);
// Return index of table entry with matching data:
// apparent endpoint
// actual endpoint
// protocol
Boolean WriteEntry(translationEntry_t table[], translationEntry_t* mapData);
// Update the flags or other data associated with a NAT table entry.
// To delete an entry, the corresponding flag is set.
// To load a new entry, use entryID=0. Returns false if table is full.
void DeleteEntry(translationEntry_t table[], UInt16 index, UInt16 prev);
UInt32 AgeTable(translationEntry_t table[], UInt16 timeOut);
// Age every NAT entry. Delete any that exceed timeOut.
// Could be invoked by ioctl from IPNLink application to
// keep the table up to date. Return the number of active
// entries in the table (so we can track if the table becomes
// full, and when entries are aged out).
#endif